package com.audaque.springboot.foshanupload.authcore.service.impl;

import com.audaque.springboot.foshanupload.authcore.model.currentUser.TransmittableThreadLocalCurrentUser;
import com.audaque.springboot.foshanupload.authcore.service.inface.LoginService;
import com.audaque.springboot.foshanupload.core.enums.ResultCodeEnum;
import com.audaque.springboot.foshanupload.core.result.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author zgb
 * @desc 登录状态同步到TransmittableThreadLocalCurrentUser
 * @date 2022-07-25 11:01:16
 */

public abstract   class LoginServiceImpl implements LoginService {

    public String getSessionId(HttpServletRequest request, HttpSession httpSession) {
        HttpSession session = request.getSession();
        String id = null;
        if(session!=null){
            id =  session.getId();
        }
        return id;
    }

    @Override
    public String getUserId() {
        TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = this.getCurrentUser(  );
        String userId =null;
        String username = null;
        if(transmittableThreadLocalCurrentUser!=null){
             userId = transmittableThreadLocalCurrentUser.getId();
             username = transmittableThreadLocalCurrentUser.getUsername();
        }else{
          throw new RuntimeException(ResultCodeEnum.NOT_LOGIN.getDescription());
        }

        return userId;
    }




    public TransmittableThreadLocalCurrentUser getCurrentUser() {
        TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = TransmittableThreadLocalCurrentUser.getCurrentUser();
        transmittableThreadLocalCurrentUser.setPassword(null);
        return transmittableThreadLocalCurrentUser;
    }

    public R checkLogin() {
        R r = new R();
        TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = this.getCurrentUser(  );

        if (transmittableThreadLocalCurrentUser != null) {
            transmittableThreadLocalCurrentUser.setPassword(null);
            r.setSuccess(true);
            r.setData(transmittableThreadLocalCurrentUser);
            r.setStatus(ResultCodeEnum.SUCCESS.getStatus());
            r.setDescription(ResultCodeEnum.SUCCESS.getDescription());
        }else {
            r.setSuccess(false);
            r.setStatus(ResultCodeEnum.NOT_LOGIN.getStatus());
            r.setDescription(ResultCodeEnum.NOT_LOGIN.getDescription());
        }


        return r;
    }




}
